package com.szly.phm.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szly.phm.common.entity.dto.ta.*;
import com.szly.phm.common.entity.dto.user.LoginUserDTO;
import com.szly.phm.common.entity.po.*;
import com.szly.phm.common.entity.result.CodeStatusEnum;
import com.szly.phm.common.entity.result.PageResult;
import com.szly.phm.common.entity.result.RootResponse;
import com.szly.phm.common.entity.vo.ta.TaLifePredictionModelVO;
import com.szly.phm.common.entity.vo.ta.TaLifePredictionVO;
import com.szly.phm.common.entity.vo.ta.TaTaskVO;
import com.szly.phm.common.mapper.*;
import com.szly.phm.common.service.ITaService;
import com.szly.phm.common.utils.BeanCopyUtils;
import com.szly.phm.common.utils.DateUtils;
import com.szly.phm.common.utils.HandleUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2021-09-19
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TaServiceImpl implements ITaService {
    private final TaExpertKnowledgeLibraryMapper taExpertKnowledgeLibraryMapper;
    private final TaResultMapper taResultMapper;
    private final TaIntelligenceModelMapper taIntelligenceLibraryMapper;

    private final CbandRealdataMapper cbandRealdataMapper;
    private final TaAlgorithmlLibraryMapper taAlgorithmlLibraryMapper;

    private final TaAlgorithmModelLibraryMapper taAlgorithmModelLibraryMapper;

    private final TaTaskTypeMapper taTaskTypeMapper;
    private final TaAlgorithmExpertKnowledgeMapper taAlgorithmExpertKnowledgeMapper;

    private final TaTaskMapper taTaskMapper;

    private final RaRadarMapper raRadarMapper;

    private final RaSystemMapper raSystemMapper;

    private final RaExtensionMapper raExtensionMapper;

    private final RaKeyPartsModelMapper raKeyPartsModelMapper;
    @Override
    public RootResponse createTaExpertKnowledgeLibrarys(CreateTaExpertKnowledgeLibrarysDTO createRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<TaExpertKnowledgeLibrary>();
        wrapper.eq(TaExpertKnowledgeLibrary::getIsSoftDelete, false);
        wrapper.eq(TaExpertKnowledgeLibrary::getName,createRaInterventionTypeDTO.getName());
        var taExpertKnowledgeLibrary=taExpertKnowledgeLibraryMapper.selectOne(wrapper);
        if(taExpertKnowledgeLibrary!=null){
            return RootResponse.error("名称已存在！！！");
        }
        var fiRadarType= new TaExpertKnowledgeLibrary();
        BeanCopyUtils.copy(createRaInterventionTypeDTO,fiRadarType);
        fiRadarType.setId(HandleUtils.UUID32());
        fiRadarType.setIsSoftDelete(false);
        fiRadarType.setCreator(userDTO.getUsername());
        fiRadarType.setCreateDateTime(new Date());
        int i =  taExpertKnowledgeLibraryMapper.insert(fiRadarType);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectTaExpertKnowledgeLibrarys(TaExpertKnowledgeLibrarysPageDTO raRadarsPageDTO) {
        if("01".equals(raRadarsPageDTO.getType())){
            var queryWrapper = new LambdaQueryWrapper<TaExpertKnowledgeLibrary>();
            queryWrapper.eq(TaExpertKnowledgeLibrary::getIsSoftDelete, false);
            queryWrapper.orderByDesc(TaExpertKnowledgeLibrary::getCreateDateTime);
            return RootResponse.success(taExpertKnowledgeLibraryMapper.selectList(queryWrapper)) ;
        }else {
            PageHelper.startPage(raRadarsPageDTO.getPageNum(), raRadarsPageDTO.getPageSize());
            var queryWrapper = new LambdaQueryWrapper<TaExpertKnowledgeLibrary>();
            queryWrapper.eq(TaExpertKnowledgeLibrary::getIsSoftDelete, false);
            if(raRadarsPageDTO.getName()!=null){
                queryWrapper.like(TaExpertKnowledgeLibrary::getName, raRadarsPageDTO.getName());
            }
            queryWrapper.orderByDesc(TaExpertKnowledgeLibrary::getCreateDateTime);
            var raserverAreas= taExpertKnowledgeLibraryMapper.selectList(queryWrapper);
            var pageInfo = PageInfo.of(raserverAreas);

            return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

        }

    }

    @Override
    public RootResponse deleteTaExpertKnowledgeLibrarys(String id, LoginUserDTO userDTO) {
        var modelLibraryLambdaQueryWrapper = new LambdaQueryWrapper<TaAlgorithmExpertKnowledge>();
        modelLibraryLambdaQueryWrapper.eq(TaAlgorithmExpertKnowledge::getIsSoftDelete,false);
        modelLibraryLambdaQueryWrapper.eq(TaAlgorithmExpertKnowledge::getExpertKnowledgeId,id);
        if (taAlgorithmExpertKnowledgeMapper.selectList(modelLibraryLambdaQueryWrapper).size()!=0){
            return RootResponse.error("该专家知识库已被绑定，不能删除");
        }
        var raServerType=  new TaExpertKnowledgeLibrary();
        raServerType.setUpdater(userDTO.getUsername());
        raServerType.setUpdateDateTime(new Date());
        raServerType.setId(id);
        raServerType.setIsSoftDelete(true);
        int i =  taExpertKnowledgeLibraryMapper.updateById(raServerType);
        return i == 1 ? RootResponse.success(taExpertKnowledgeLibraryMapper.selectById(id)) : RootResponse.error();
    }

    @Override
    public RootResponse updateTaExpertKnowledgeLibrarys(UpdateTaExpertKnowledgeLibrarysDTO updateRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<TaExpertKnowledgeLibrary>();
        wrapper.eq(TaExpertKnowledgeLibrary::getIsSoftDelete, false);
        wrapper.eq(TaExpertKnowledgeLibrary::getName,updateRaInterventionTypeDTO.getName());
       var taExpertKnowledgeLibrary= taExpertKnowledgeLibraryMapper.selectOne(wrapper);
        if(taExpertKnowledgeLibrary!=null&&!taExpertKnowledgeLibrary.getId().equals(updateRaInterventionTypeDTO.getId())){
            return RootResponse.error("名称已存在！！！");
        }
        var raserverArea=new TaExpertKnowledgeLibrary();
        BeanCopyUtils.copy(updateRaInterventionTypeDTO,raserverArea);
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        int i =taExpertKnowledgeLibraryMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse createTaIntelligenceLibrarys(CreateTaIntelligenceLibrarysDTO createRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<TaIntelligenceLibrary>();
        wrapper.eq(TaIntelligenceLibrary::getIsSoftDelete, false);
        wrapper.and(wrapper1 -> wrapper1.eq(TaIntelligenceLibrary::getCName, createRaInterventionTypeDTO.getCName()).or().eq(TaIntelligenceLibrary::getEName, createRaInterventionTypeDTO.getEName()));
        var taExpertKnowledgeLibrary=taIntelligenceLibraryMapper.selectOne(wrapper);
        if(taExpertKnowledgeLibrary!=null){
            return RootResponse.error("名称已存在！！！");
        }
        var fiRadarType= new TaIntelligenceLibrary();
        BeanCopyUtils.copy(createRaInterventionTypeDTO,fiRadarType);
        fiRadarType.setId(HandleUtils.UUID32());
        fiRadarType.setIsSoftDelete(false);
        fiRadarType.setCreator(userDTO.getUsername());
        fiRadarType.setCreateDateTime(new Date());
        int i =  taIntelligenceLibraryMapper.insert(fiRadarType);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectTaIntelligenceLibrarys(TaIntelligenceLibrarysPageDTO raRadarsPageDTO) {
        PageHelper.startPage(raRadarsPageDTO.getPageNum(), raRadarsPageDTO.getPageSize());
        var queryWrapper = new LambdaQueryWrapper<TaIntelligenceLibrary>();
        queryWrapper.eq(TaIntelligenceLibrary::getIsSoftDelete, false);
        if(raRadarsPageDTO.getName()!=null){
            queryWrapper.and(wrapper1 -> wrapper1.like(TaIntelligenceLibrary::getCName, raRadarsPageDTO.getName()).or().like(TaIntelligenceLibrary::getEName, raRadarsPageDTO.getName()));
        }
        if(raRadarsPageDTO.getIsUse()!=null){
            queryWrapper.eq(TaIntelligenceLibrary::getIsUse,raRadarsPageDTO.getIsUse());

        }
        queryWrapper.orderByDesc(TaIntelligenceLibrary::getCreateDateTime);
        var raserverAreas= taIntelligenceLibraryMapper.selectList(queryWrapper);
        var pageInfo = PageInfo.of(raserverAreas);

        return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse deleteTaIntelligenceLibrarys(String id, LoginUserDTO userDTO) {
        var modelLibraryLambdaQueryWrapper = new LambdaQueryWrapper<TaAlgorithmlLibrary>();
        modelLibraryLambdaQueryWrapper.eq(TaAlgorithmlLibrary::getIsSoftDelete,false);
        modelLibraryLambdaQueryWrapper.eq(TaAlgorithmlLibrary::getIntelligencelId,id);
        if (taAlgorithmlLibraryMapper.selectList(modelLibraryLambdaQueryWrapper).size()!=0){
            return RootResponse.error("该智能库已被算法库绑定，不能删除");
        }
        var raServerType=  new TaIntelligenceLibrary();
        raServerType.setUpdater(userDTO.getUsername());
        raServerType.setUpdateDateTime(new Date());
        raServerType.setId(id);
        raServerType.setIsSoftDelete(true);
        int i =  taIntelligenceLibraryMapper.updateById(raServerType);
        return i == 1 ? RootResponse.success(taIntelligenceLibraryMapper.selectById(id)) : RootResponse.error();
    }

    @Override
    public RootResponse updateTaIntelligenceLibrarys(UpdateTaIntelligenceLibrarysDTO updateRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<TaIntelligenceLibrary>();
        wrapper.eq(TaIntelligenceLibrary::getIsSoftDelete, false);
        wrapper.and(wrapper1 -> wrapper1.eq(TaIntelligenceLibrary::getCName, updateRaInterventionTypeDTO.getCName()).or().eq(TaIntelligenceLibrary::getEName, updateRaInterventionTypeDTO.getEName()));
        var taIntelligenceLibrary=taIntelligenceLibraryMapper.selectList(wrapper);
        if(taIntelligenceLibrary!=null&&taIntelligenceLibrary.size()>1){
            return RootResponse.error("名称已存在！！！");
        }
        if(taIntelligenceLibrary.size()!=0&&!taIntelligenceLibrary.get(0).getId().equals(updateRaInterventionTypeDTO.getId())){
            return RootResponse.error("名称已存在！！！");
        }
        var raserverArea=new TaIntelligenceLibrary();
        BeanCopyUtils.copy(updateRaInterventionTypeDTO,raserverArea);
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        int i =taIntelligenceLibraryMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse createTaAlgorithmlLibrarys(CreateTaAlgorithmlLibrarysDTO createRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<TaAlgorithmlLibrary>();
        wrapper.eq(TaAlgorithmlLibrary::getIsSoftDelete, false);
        wrapper.and(wrapper1 -> wrapper1.eq(TaAlgorithmlLibrary::getCName, createRaInterventionTypeDTO.getCName()).or().eq(TaAlgorithmlLibrary::getEName, createRaInterventionTypeDTO.getEName()));
        var taExpertKnowledgeLibrary=taAlgorithmlLibraryMapper.selectOne(wrapper);
        if(taExpertKnowledgeLibrary!=null){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");
        }
        var fiRadarType= new TaAlgorithmlLibrary();
        BeanCopyUtils.copy(createRaInterventionTypeDTO,fiRadarType);
        fiRadarType.setId(HandleUtils.UUID32());
        fiRadarType.setIsSoftDelete(false);
        fiRadarType.setCreator(userDTO.getUsername());
        fiRadarType.setCreateDateTime(new Date());
        int i =  taAlgorithmlLibraryMapper.insert(fiRadarType);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectTaAlgorithmlLibrarys(TaAlgorithmlLibrarysPageDTO raRadarsPageDTO) {
        PageHelper.startPage(raRadarsPageDTO.getPageNum(), raRadarsPageDTO.getPageSize());

        var raserverAreas= taAlgorithmlLibraryMapper.selectAlgorithmlLibrary(raRadarsPageDTO);
        var pageInfo = PageInfo.of(raserverAreas);

        return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse deleteTaAlgorithmlLibrarys(String id, LoginUserDTO userDTO) {
        var modelLibraryLambdaQueryWrapper = new LambdaQueryWrapper<TaAlgorithmModelLibrary>();
        modelLibraryLambdaQueryWrapper.eq(TaAlgorithmModelLibrary::getIsSoftDelete,false);
        modelLibraryLambdaQueryWrapper.eq(TaAlgorithmModelLibrary::getAlgorithmId,id);
        if (taAlgorithmModelLibraryMapper.selectList(modelLibraryLambdaQueryWrapper).size()!=0){
            return RootResponse.error("该算法库已被算法模型库绑定，不能删除");
        }
        var raServerType=  new TaAlgorithmlLibrary();
        raServerType.setUpdater(userDTO.getUsername());
        raServerType.setUpdateDateTime(new Date());
        raServerType.setId(id);
        raServerType.setIsSoftDelete(true);
        int i =  taAlgorithmlLibraryMapper.updateById(raServerType);
        return i == 1 ? RootResponse.success(taAlgorithmlLibraryMapper.selectById(id)) : RootResponse.error();
    }

    @Override
    public RootResponse updateTaAlgorithmlLibrarys(UpdateTaAlgorithmlLibrarysDTO updateRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<TaAlgorithmlLibrary>();
        wrapper.eq(TaAlgorithmlLibrary::getIsSoftDelete, false);
       wrapper.and(wrapper1 -> wrapper1.eq(TaAlgorithmlLibrary::getCName, updateRaInterventionTypeDTO.getCName()).or().eq(TaAlgorithmlLibrary::getEName, updateRaInterventionTypeDTO.getEName()));
        var taAlgorithmlLibrary=taAlgorithmlLibraryMapper.selectList(wrapper);
        if(taAlgorithmlLibrary!=null&&taAlgorithmlLibrary.size()>1){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");

        }
        if(taAlgorithmlLibrary.size()!=0&&!taAlgorithmlLibrary.get(0).getId().equals(updateRaInterventionTypeDTO.getId())){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");
        }

        var raserverArea=new TaAlgorithmlLibrary();
        BeanCopyUtils.copy(updateRaInterventionTypeDTO,raserverArea);
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        int i =taAlgorithmlLibraryMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse createTaAlgorithmModelLibrarys(CreateTaAlgorithmModelLibrarysDTO createRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<TaAlgorithmModelLibrary>();
        wrapper.eq(TaAlgorithmModelLibrary::getIsSoftDelete, false);
        wrapper.and(wrapper1 -> wrapper1.eq(TaAlgorithmModelLibrary::getCName, createRaInterventionTypeDTO.getCName()).or().eq(TaAlgorithmModelLibrary::getEName, createRaInterventionTypeDTO.getEName()));
        var taExpertKnowledgeLibrary=taAlgorithmModelLibraryMapper.selectList(wrapper);
        if(!taExpertKnowledgeLibrary.isEmpty()){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");
        }
        var fiRadarType= new TaAlgorithmModelLibrary();
        BeanCopyUtils.copy(createRaInterventionTypeDTO,fiRadarType);
        fiRadarType.setId(HandleUtils.UUID32());
        fiRadarType.setIsSoftDelete(false);
        fiRadarType.setCreator(userDTO.getUsername());
        fiRadarType.setCreateDateTime(new Date());
        int i =  taAlgorithmModelLibraryMapper.insert(fiRadarType);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectTaAlgorithmModelLibrarys(TaAlgorithmModelLibrarysPageDTO raRadarsPageDTO) {
        PageHelper.startPage(raRadarsPageDTO.getPageNum(), raRadarsPageDTO.getPageSize());

        var raserverAreas= taAlgorithmModelLibraryMapper.selectTaAlgorithmModelLibraryMapper(raRadarsPageDTO);
        var pageInfo = PageInfo.of(raserverAreas);
        pageInfo.getList().forEach(t->{
            var getAlgorithmModelObjectName="";
            if(t.getAlgorithmModelObject()!=null&&!"".equals(t.getAlgorithmModelObject())){
                var ids=t.getAlgorithmModelObject().split("-");
                for(int i=0;i<ids.length;i++){
                    if(i==0){
                        var raSystem=raSystemMapper.selectById(ids[i]);
                        if(raSystem!=null){
                            getAlgorithmModelObjectName+=raSystem.getName();
                        }

                    }
                    if(i==1){
                        var raExtension=raExtensionMapper.selectById(ids[i]);
                        if(raExtension!=null){
                            getAlgorithmModelObjectName+="-"+raExtensionMapper.selectById(ids[i]).getName();
                        }
                    }
                    if(i==2){
                        var raKeyPartsModel=raKeyPartsModelMapper.selectById(ids[i]);
                        if(raKeyPartsModel!=null){
                            getAlgorithmModelObjectName+="-"+raKeyPartsModelMapper.selectById(ids[i]).getName();
                        }
                    }
                }
            }
            t.setAlgorithmModelObjectName(getAlgorithmModelObjectName);
            var taTaskType=taTaskTypeMapper.selectById(t.getTaskTypeId());
            t.setTaskTypeName(taTaskType==null?null:taTaskType.getName());
        });
        return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse deleteTaAlgorithmModelLibrarys(String id, LoginUserDTO userDTO) {
        var modelLibraryLambdaQueryWrapper = new LambdaQueryWrapper<TaTask>();
        modelLibraryLambdaQueryWrapper.eq(TaTask::getIsSoftDelete,false);
        modelLibraryLambdaQueryWrapper.eq(TaTask::getAlgorithmModelId,id);
        if (taTaskMapper.selectList(modelLibraryLambdaQueryWrapper).size()!=0){
            return RootResponse.error("该算法模型已被任务信息绑定，不能删除");
        }
        var raServerType=  new TaAlgorithmModelLibrary();
        raServerType.setUpdater(userDTO.getUsername());
        raServerType.setUpdateDateTime(new Date());
        raServerType.setId(id);
        raServerType.setIsSoftDelete(true);
        int i =  taAlgorithmModelLibraryMapper.updateById(raServerType);
        return i == 1 ? RootResponse.success(taAlgorithmModelLibraryMapper.selectById(id)) : RootResponse.error();
    }

    @Override
    public RootResponse updateTaAlgorithmModelLibrarys(UpdateTaAlgorithmModelLibrarysDTO updateRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<TaAlgorithmModelLibrary>();
        wrapper.eq(TaAlgorithmModelLibrary::getIsSoftDelete, false);
        wrapper.and(wrapper1 -> wrapper1.eq(TaAlgorithmModelLibrary::getCName, updateRaInterventionTypeDTO.getCName()).or().eq(TaAlgorithmModelLibrary::getEName, updateRaInterventionTypeDTO.getEName()));
       var taAlgorithmModelLibrary= taAlgorithmModelLibraryMapper.selectList(wrapper);
        if(taAlgorithmModelLibrary!=null&&taAlgorithmModelLibrary.size()>1){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");

        }

        if(taAlgorithmModelLibrary.size()!=0&&!taAlgorithmModelLibrary.get(0).getId().equals(updateRaInterventionTypeDTO.getId())){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");
        }
        var raserverArea=new TaAlgorithmModelLibrary();
        BeanCopyUtils.copy(updateRaInterventionTypeDTO,raserverArea);
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        int i =taAlgorithmModelLibraryMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse createTaTaskTypes(CreateTaTaskTypesDTO createRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<TaTaskType>();
        wrapper.eq(TaTaskType::getIsSoftDelete, false);
        wrapper.eq(TaTaskType::getName,createRaInterventionTypeDTO.getName());
        var taExpertKnowledgeLibrary=taTaskTypeMapper.selectOne(wrapper);
        if(taExpertKnowledgeLibrary!=null){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");
        }
        var fiRadarType= new TaTaskType();
        BeanCopyUtils.copy(createRaInterventionTypeDTO,fiRadarType);
        fiRadarType.setId(HandleUtils.UUID32());
        fiRadarType.setIsSoftDelete(false);
        fiRadarType.setCreator(userDTO.getUsername());
        fiRadarType.setCreateDateTime(new Date());
        int i =  taTaskTypeMapper.insert(fiRadarType);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectTaTaskTypes(TaTaskTypesPageDTO raRadarsPageDTO) {
        PageHelper.startPage(raRadarsPageDTO.getPageNum(), raRadarsPageDTO.getPageSize());
        var queryWrapper = new LambdaQueryWrapper<TaTaskType>();
        queryWrapper.eq(TaTaskType::getIsSoftDelete, false);
        if(raRadarsPageDTO.getName()!=null){
            queryWrapper.like(TaTaskType::getName, raRadarsPageDTO.getName());
        }
        queryWrapper.orderByDesc(TaTaskType::getCreateDateTime);
        var raserverAreas= taTaskTypeMapper.selectList(queryWrapper);
        var pageInfo = PageInfo.of(raserverAreas);

        return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse deleteTaTaskTypes(String id, LoginUserDTO userDTO) {
        var taTaskLambdaQueryWrapper = new LambdaQueryWrapper<TaTask>();
        taTaskLambdaQueryWrapper.eq(TaTask::getIsSoftDelete,false);
        taTaskLambdaQueryWrapper.eq(TaTask::getTypeId,id);
        if (taTaskMapper.selectList(taTaskLambdaQueryWrapper).size()!=0){
            return RootResponse.error("该类型已被任务基础信息绑定，不能删除");
        }
        var taAlgorithmModelLibraryLambdaQueryWrapper = new LambdaQueryWrapper<TaAlgorithmModelLibrary>();
        taAlgorithmModelLibraryLambdaQueryWrapper.eq(TaAlgorithmModelLibrary::getIsSoftDelete,false);
        taAlgorithmModelLibraryLambdaQueryWrapper.eq(TaAlgorithmModelLibrary::getTaskTypeId,id);
        if (taAlgorithmModelLibraryMapper.selectList(taAlgorithmModelLibraryLambdaQueryWrapper).size()!=0){
            return RootResponse.error("该类型已被算法模型绑定，不能删除");
        }
        var raServerType=  new TaTaskType();
        raServerType.setUpdater(userDTO.getUsername());
        raServerType.setUpdateDateTime(new Date());
        raServerType.setId(id);
        raServerType.setIsSoftDelete(true);
        int i =  taTaskTypeMapper.updateById(raServerType);
        return i == 1 ? RootResponse.success(taAlgorithmModelLibraryMapper.selectById(id)) : RootResponse.error();

    }

    @Override
    public RootResponse updateTaTaskTypes(UpdateTaTaskTypesDTO updateRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<TaTaskType>();
        wrapper.eq(TaTaskType::getIsSoftDelete, false);
        wrapper.eq(TaTaskType::getName,updateRaInterventionTypeDTO.getName());
        var taTaskType= taTaskTypeMapper.selectOne(wrapper);
        if(taTaskType!=null&&!taTaskType.getId().equals(updateRaInterventionTypeDTO.getId())){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");
        }
        var raserverArea=new TaTaskType();
        BeanCopyUtils.copy(updateRaInterventionTypeDTO,raserverArea);
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        int i =taTaskTypeMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectTaAlgorithmExpertKnowledges(String algorithmId) {

        return  RootResponse.success(taAlgorithmExpertKnowledgeMapper.selectAlgorithmExpertKnowledgeByAlgorithmId(algorithmId));
    }

    @Override
    public RootResponse createTaAlgorithmExpertKnowledges(CreateTaAlgorithmExpertKnowledgesDTO createRaInterventionTypeDTO, LoginUserDTO userDTO) {

        // 先删除角色和权限的关联关系
        var wrapper = new LambdaQueryWrapper<TaAlgorithmExpertKnowledge>();
        wrapper.eq(TaAlgorithmExpertKnowledge::getAlgorithmId, createRaInterventionTypeDTO.getAlgorithmId());
        taAlgorithmExpertKnowledgeMapper.delete(wrapper);
        // 添加关联关系
        if (createRaInterventionTypeDTO.getExpertKnowledgeIds() != null && !createRaInterventionTypeDTO.getExpertKnowledgeIds().isEmpty()) {
            createRaInterventionTypeDTO.getExpertKnowledgeIds().forEach(m -> {
                var sysRolePermission= new TaAlgorithmExpertKnowledge();
                sysRolePermission.setId(HandleUtils.UUID32());
                sysRolePermission.setCreator(userDTO.getUsername());
                sysRolePermission.setCreateDateTime(new Date());
                sysRolePermission.setExpertKnowledgeId(m);
                sysRolePermission.setIsSoftDelete(false);
                sysRolePermission.setAlgorithmId(createRaInterventionTypeDTO.getAlgorithmId());
                int j=taAlgorithmExpertKnowledgeMapper.insert(sysRolePermission);
                if (j != 1) {
                    throw new RuntimeException("失败");
                }
            });
        }
        return RootResponse.success();
    }

    @Override
    public RootResponse createTaTasks(CreateTaTasksDTO createRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var taTaskLambdaQueryWrapper=new LambdaQueryWrapper<TaTask>();
        taTaskLambdaQueryWrapper.eq(TaTask::getIsSoftDelete,0);
        taTaskLambdaQueryWrapper.eq(TaTask::getAlgorithmModelId,createRaInterventionTypeDTO.getAlgorithmModelId());
        taTaskLambdaQueryWrapper.eq(TaTask::getTreeId,createRaInterventionTypeDTO.getTreeId());
        if (taTaskMapper.selectList(taTaskLambdaQueryWrapper).size()!=0){
            return RootResponse.error("不能绑定相同的算法模型！！！");
        }
        var fiRadarType= new TaTask();
        BeanCopyUtils.copy(createRaInterventionTypeDTO,fiRadarType);
        fiRadarType.setId(HandleUtils.UUID32());
        fiRadarType.setIsSoftDelete(false);
        fiRadarType.setCreator(userDTO.getUsername());
        fiRadarType.setCreateDateTime(new Date());
        int i =  taTaskMapper.insert(fiRadarType);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectTaTasks(TaTasksPageDTO raRadarsPageDTO) {
        PageHelper.startPage(raRadarsPageDTO.getPageNum(), raRadarsPageDTO.getPageSize());
        var newList=new ArrayList<TaTaskVO>();
        var taTaskVOS= taTaskMapper.selectTaTask(raRadarsPageDTO);
        var pageInfo = PageInfo.of(taTaskVOS);
        pageInfo.getList().forEach(p->{
            String s="";
            var taTaskVO=new TaTaskVO();
            BeanCopyUtils.copy(p,taTaskVO);
            if(taTaskVO.getTreeId()!=null){
                if(taTaskVO.getRadarId()!=null){
                    s+=raRadarMapper.selectById(taTaskVO.getRadarId()).getName();
                }
                if(taTaskVO.getSystemId()!=null){
                    s+="-"+raSystemMapper.selectById(taTaskVO.getSystemId()).getName();
                }
                if(taTaskVO.getExtensionId()!=null){
                    s+="-"+raExtensionMapper.selectById(taTaskVO.getExtensionId()).getName();
                }
                if(taTaskVO.getKeyPartsModelId()!=null){
                    s+="-"+raKeyPartsModelMapper.selectById(taTaskVO.getKeyPartsModelId()).getName();
                }

            }
            taTaskVO.setTreeName(s);
            newList.add(taTaskVO);
        });
        return RootResponse.success(PageResult.page(newList, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse deleteTaTasks(String id, LoginUserDTO userDTO) {
        var raServerType=  new TaTask();
        raServerType.setUpdater(userDTO.getUsername());
        raServerType.setUpdateDateTime(new Date());
        raServerType.setId(id);
        raServerType.setIsSoftDelete(true);
        int i =  taTaskMapper.updateById(raServerType);
        return i == 1 ? RootResponse.success(taTaskMapper.selectById(id)) : RootResponse.error();
    }

    @Override
    public RootResponse updateTaTasks(UpdateTaTasksDTO updateRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var raserverArea=new TaTask();
        BeanCopyUtils.copy(updateRaInterventionTypeDTO,raserverArea);
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        int i =taTaskMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectTaTasksResult(TaTasksResultPageDTO serverAreaPageDTO) {
        PageHelper.startPage(serverAreaPageDTO.getPageNum(), serverAreaPageDTO.getPageSize());
        var taResultVOS= taResultMapper.selectTaResult(serverAreaPageDTO);
        var pageInfo = PageInfo.of(taResultVOS);
        return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));
    }

    @Override
    public RootResponse selectLifePrediction(String radarId) {
        Map<String, Object> map = cbandRealdataMapper.selectRealFieldTa(raRadarMapper.selectById(radarId).getDataTableName());

        String strDateFormat = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
       var taLifePredictions= new ArrayList<TaLifePredictionVO>();
        var mapList=taAlgorithmExpertKnowledgeMapper.selectLifeTime(radarId);
        for(Map<String,Object> m :mapList){
            var date =new Date();
            var taLifePrediction= new TaLifePredictionVO();
            taLifePrediction.setName(m.get("name").toString());
            taLifePrediction.setTypeName(taAlgorithmExpertKnowledgeMapper.selectRaKeyPartsTypeName(radarId,m.get("name").toString()).get("typeName").toString());
            taLifePrediction.setTotalLife(m.get("value").toString());
            taLifePrediction.setActiveLife(new BigDecimal(Float.valueOf(map.get("HighVoltTime").toString())/(float)3600).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
            taLifePrediction.setResidualLife(Float.valueOf(m.get("value").toString())-taLifePrediction.getActiveLife());
            for(int i=0;i<taLifePrediction.getResidualLife()/8;i++){
                if(4<Integer.valueOf(sdf.format(date).split("-")[2])&&Integer.valueOf(sdf.format(date).split("-")[2])<11){
                    i=i+2;
                    date=DateUtils.addDay(date,1);
                }else {
                    date=DateUtils.addDay(date,1);
                }
            }
            taLifePrediction.setFailureTime(sdf.format(date));
            taLifePredictions.add(taLifePrediction);

        }

        return RootResponse.success(taLifePredictions);
    }

    @Override
    public RootResponse selectLifePieceModel(LifePieceModelDTO lifePieceModelDTO) {

        Map<String, Object> map = cbandRealdataMapper.selectRealFieldTa(raRadarMapper.selectById(lifePieceModelDTO.getRadarId()).getDataTableName());

        String strDateFormat = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        var taLifePredictionModelVOS= new ArrayList<TaLifePredictionModelVO>();
        for(String id:lifePieceModelDTO.getIds()){
            var date =new Date();
            var taLifePrediction= new TaLifePredictionModelVO();
            var taAlgorithmModelLibrary=taAlgorithmModelLibraryMapper.selectById(id);
            var raKeyPartsModel=taAlgorithmExpertKnowledgeMapper.selectRaKeyParts(lifePieceModelDTO.getKeyPartsModelId());
            taLifePrediction.setName(raKeyPartsModel.get("name").toString());
            taLifePrediction.setTypeName(raKeyPartsModel.get("typeName").toString());
            taLifePrediction.setTaAlgorithmModelName(taAlgorithmModelLibrary==null?"":taAlgorithmModelLibrary.getCName());
            var m=taAlgorithmExpertKnowledgeMapper.selectLifeTimeByKeyPartsModel(lifePieceModelDTO.getKeyPartsModelId());
            taLifePrediction.setTotalLife(m.get("value").toString());
            taLifePrediction.setActiveLife(new BigDecimal(Float.valueOf(map.get("HighVoltTime").toString())/(float)3600).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
           var map1= taAlgorithmExpertKnowledgeMapper.selectTaskResult(id,lifePieceModelDTO.getKeyPartsModelId());
            taLifePrediction.setResidualLife(Float.valueOf(m.get("value").toString())-Float.valueOf(map1.get("result_data").toString()));
            taLifePrediction.setConfidence(Float.valueOf(map1.get("trust_degree").toString()));
            for(int i=0;i<taLifePrediction.getResidualLife()/8;i++){
                if(4<Integer.valueOf(sdf.format(date).split("-")[2])&&Integer.valueOf(sdf.format(date).split("-")[2])<11){
                    i=i+2;
                    date=DateUtils.addDay(date,1);
                }else {
                    date=DateUtils.addDay(date,1);
                }
            }
            taLifePrediction.setFailureTime(sdf.format(date));
            taLifePredictionModelVOS.add(taLifePrediction);
        }
        return RootResponse.success(taLifePredictionModelVOS);
    }

    @Override
    public RootResponse selectLifeModel(String keyPartsModelId) {
        return RootResponse.success( taAlgorithmModelLibraryMapper.selectLifePieceModel(keyPartsModelId));
    }

}
